package cn.dapeng.common.nio;


import java.io.DataInputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class NIOServer {

    public static volatile boolean isRunning = true;

    public static void main(String[] args) throws Exception {
        start(8081);
    }

    public static void start(int port) {
        //创建serversocket 对象--8081服务
        try (ServerSocket serverSocket = new ServerSocket(port)) {
            //循环监听连接
            while (isRunning) {
                Socket socket = serverSocket.accept();//客户端发起网络请求---连接
                //创建输⼊流对象
                DataInputStream dataInputStream = new
                        DataInputStream(socket.getInputStream());
                int byteCount = 0;
                byte[] bytes = new byte[1024];        //创建缓冲区字节数组
                while (true) {
                    int readCount = dataInputStream.read(bytes, 0,
                            bytes.length);
                    byteCount = byteCount + readCount;
                    if (readCount == -1) {
                        System.out.println("服务端接受：" + byteCount + "字节");
                        break;
                    }
                }
            }
        } catch (Exception e) {
            isRunning = false;
        }
    }

}
